using System;
using System.Collections.Generic;
using System.Text;
using Lizk.SimpleHUD;
using System.Diagnostics;
using System.Collections;
/*
namespace Feditor
{
    public class ControlSelection : HUDContainerSimple
    {
        private bool handleHovered;
        private List<HUDControl> selectionStack = new List<HUDControl>();
        private HUDControl selectedControl, currentHandle;
        private RectangleS selectedBounds;
        private DateTime mouseDownTime;
        private PointS mouseDownPos;

        private enum State { None, Waiting, Selected, Resizing, Moving }
        private State state = State.None;
        public event EventHandler SelectionChanged;
        public event EventHandler SelectedControlChanged;

        public bool HandleHovered
        {
            get { return handleHovered; }
            set { handleHovered = value; }
        }
        public HUDControl SelectedControl
        {
            get { return selectedControl; }
            set {
                if (value == hud)
                    value = null;
                selectedControl = value;
                selectionStack.Clear();
                if(selectedControl != null)
                    RebuildSelectionStack(hud, selectedControl);
                if (value == null)
                    state = State.None;
                else
                {
                    state = State.Selected;
                    UpdateSelection();
                }
            }
        }
        public HUDEngine hud;
        
        public ControlSelection()
        {
            for (int i = 0; i < 8; i++)
            {
                HUDImage nw = new HUDImage("white.png");
                nw.Alpha = 200;
                nw.ConstrainProportions = false;
                nw.Bounds = new RectangleS(0, 0, 4, 4);
                Add(nw);
            }

        }

        private HUDControl RebuildSelectionStack(HUDControl current, HUDControl target)
        {
            selectionStack.Add(current);
            
            if (current == target)
                return current;

            if (current is ICollection)
            {
                foreach (HUDControl child in (ICollection)current)
                {
                    HUDControl found = RebuildSelectionStack(child, target);
                    if (found == null)
                        selectionStack.RemoveAt(selectionStack.Count - 1);
                    else
                        return found;
                }
            }
            return null;
        }

        public void Select(PointS p)
        {
        
        }


        public void UpdateInput(PointS p)
        {
            if (hud.Bounds.Contains(p) && Input.Mouse.Left == MouseInfo.ButtonState.Down)
            {
                if (state == State.Selected || state == State.None)
                {
                    mouseDownTime = DateTime.Now;
                    mouseDownPos = p;
                    state = State.Waiting;

                    if (selectedControl != null)
                    {
                        selectedBounds = SelectedControl.Bounds ;
                    }
                }
            }

            if (Input.Mouse.Left == MouseInfo.ButtonState.Up)
            {
                if (hud.Bounds.Contains(p) && state == State.Waiting)
                {
                    selectionStack.Clear();
                    HUDControl newSelection = SelectItem(hud, p);
                    
                    if (newSelection != selectedControl && SelectionChanged != null)
                    {
                        selectedControl = newSelection == hud ? null : newSelection;
                        SelectionChanged(this, EventArgs.Empty);
                    }

                    if (newSelection == hud)
                    {
                        state = State.None;
                        foreach (HUDControl c in this)
                            c.Visible = false;
                    }
                    else
                    {
                        state = State.Selected;
                    }

                    // fix select en anden der ligger samme sted.
                }
                else
                {
                    if (selectedControl != null)
                    {
                        if ((state == State.Moving || state == State.Resizing) && SelectedControlChanged != null)
                        {
                            SelectedControlChanged(this, EventArgs.Empty);
                        }
                        state = State.Selected;
                    }
                    else
                        state = State.None;
                }
                mouseDownPos = new PointS(0, 0);
                EditorWindow.Cursor = "arrow";
            }


            switch (state)
            {
                case State.None:
                    break;
                case State.Waiting:
                    if ((DateTime.Now - mouseDownTime).TotalSeconds > 0.1f && selectedControl != null)
                    {
                        if (currentHandle != null)
                        {
                            state = State.Resizing;
                            SetResizeCursor();

                        }
                        else
                        {
                            if (GetSelectedBounds().Contains(mouseDownPos))
                            {
                                state = State.Moving;
                                EditorWindow.Cursor = "move";
                            }
                            else
                            {
                                state = State.Selected;
                                EditorWindow.Cursor = "arrow";
                            }
                        }
                        
                        //state = currentHandle != null ? State.Resizing : State.Moving;
                    }
                    break;
                case State.Selected:
                    // Figure out if hovering a handle
                    HUDControl hoveredHandle = null;
                    foreach (HUDControl handle in this)
                    {
                        RectangleS check = handle.Bounds;
                        check += Location;
                        if (check.Contains(p))
                        {
                            hoveredHandle = handle;
                            currentHandle = handle;
                            handleHovered = true;
                            ((HUDImage)currentHandle).ImageColor = System.Drawing.Color.Green;
                            break;
                        }
                    }

                    if (hoveredHandle == null && currentHandle != null)
                    {
                        ((HUDImage)currentHandle).ImageColor = System.Drawing.Color.White;
                        currentHandle = null;
                        handleHovered = false;
                    }

                    break;
                case State.Resizing:
                    PointS diff = p - mouseDownPos;

                    if (currentHandle == this[1] || currentHandle == this[6]) // for vertical height change
                        diff.X = 0;

                    if (currentHandle == this[3] || currentHandle == this[4]) // for horizontal height change
                        diff.Y = 0;



                    if (currentHandle == this[0] || currentHandle == this[1] || currentHandle == this[2] || currentHandle == this[3] || currentHandle == this[5])
                    {
                        PointS locDiff = diff;
                        if (currentHandle == this[5])
                        {
                            locDiff.Y = 0;
                            diff.Y *= -1;
                        }

                        if (currentHandle == this[2])
                        {
                            locDiff.X = 0;
                            diff.X *= -1;
                        }

                        selectedControl.Location = selectedBounds.Location + locDiff;
                        //Location = selectedBounds.Location + locDiff - new PointS(2, 2);
                    }
                    else
                    {
                        diff *= -1;
                    }
                    //handles.Size = startSize - (SizeS)diff + new SizeS(4, 4);
                    selectedControl.Size = selectedBounds.Size - (SizeS)diff;

                    SizeS s = new SizeS(selectedControl.Size.Width < 0 ? 0 : selectedControl.Size.Width, selectedControl.Size.Height < 0 ? 0 : selectedControl.Size.Height);
                    selectedControl.Size = s;
                    break;
                case State.Moving:
                    selectedControl.Location = selectedBounds.Location + p - mouseDownPos;
                    Location = selectedBounds.Location + p - mouseDownPos - new PointS(2, 2);
                    break;
                default:
                    break;
            }
            if(selectedControl != null)
                UpdateSelection();
        }

        public void SetResizeCursor()
        {
            if (currentHandle == this[0] || currentHandle == this[7])
                EditorWindow.Cursor = "nwse";
            else if (currentHandle == this[1] || currentHandle == this[6])
                EditorWindow.Cursor = "ns";
            else if (currentHandle == this[2] || currentHandle == this[5])
                EditorWindow.Cursor = "nesw";
            else if (currentHandle == this[3] || currentHandle == this[4])
                EditorWindow.Cursor = "ew";
        }


        private HUDControl SelectItem(HUDControl c, PointS p)
        {
            selectionStack.Add(c);
            if (c is ICollection)
            {
                foreach (HUDControl child in (ICollection)c)
                {
                    if (child.Bounds.Contains(p))
                        return SelectItem(child, p);
                }
                return c;
            }
            else
            {
                return c;
            }
        }

        private RectangleS GetSelectedBounds()
        {
            RectangleS bounds = RectangleS.Empty;
            foreach (HUDControl c in selectionStack)
                bounds += c.Location;

            bounds.Size = selectedControl.Size;
            return bounds;
        }

        private void UpdateSelection()
        {
            RectangleS bounds = GetSelectedBounds();

            this.Bounds = new RectangleS(bounds.Left - 2, bounds.Top - 2, bounds.Width + 4, bounds.Height + 4);
            this[1].Location = new PointS(Bounds.Width / 2f - 2, 0);
            this[2].Location = new PointS(Bounds.Width - 4, 0);
            this[3].Location = new PointS(0, Bounds.Height / 2f - 2);
            this[4].Location = new PointS(Bounds.Width - 4, Bounds.Height / 2f - 2);
            this[5].Location = new PointS(0, Bounds.Height - 4);
            this[6].Location = new PointS(Bounds.Width / 2f - 2, Bounds.Height - 4);
            this[7].Location = new PointS(Bounds.Width - 4, Bounds.Height - 4);
            foreach (HUDControl c in this)
                c.Visible = true;
        }

    }
}
*/